package com.skyworth.web.front;

import com.skyworth.utils.CookieUtil;
import com.skyworth.utils.StrUtil;
import com.skyworth.web.admin.table.service.ITableService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.service.UserService;
import com.skyworth.web.common.Constants;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.order.service.NewOrderService;
import com.skyworth.web.waiter.WaiterCookie;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by skyworth on 2016/10/10.
 */
@Controller
@RequestMapping("/front")
public class FronterController extends BaseController{
	Logger LOG = LoggerFactory.getLogger(FronterController.class);

	@Autowired
	UserService userService;

	@Autowired
	private ITableService iTableService;

	@Autowired
	private NewOrderService newOrderService;

	@RequestMapping("table_index")
	public String index(Model model, HttpServletRequest request, HttpServletResponse response){

		boolean isLogin = false;
		UserModel userModel = currentUser();

		try{
			if(userModel == null){
				//cookies 是否有记录，有的话登录，没有弹出登录
				String username = CookieUtil.getCookie(Constants.COOKIE_USER_KEY,request);
				username = URLDecoder.decode(username,"UTF-8");
				if(!StrUtil.isEmpty(username)){
					String password = CookieUtil.getCookie("md",request); //md5密码
					UsernamePasswordToken token = new UsernamePasswordToken(username, password);
					isLogin = isLogin(token);

					if(isLogin){
						userModel = userService.findUserByUsername(username);
					}
				}
			}else{
				isLogin = true;
			}
		}catch (Exception e){
			e.printStackTrace();
			isLogin = false;
		}

		if(!isLogin){
			WaiterCookie.writeErrorCookie(response);
		}


		return "front/front_tables";
	}
	@RequestMapping("order_index")
	public String getOrders(Model model) {
		return "front/front_orders";
	}

	@RequestMapping("table/{table_id}")
	public String getTables(Model model, @PathVariable("table_id")long table_id) {
		model.addAttribute("table_id", table_id);
		return "front/front_table_orders";
	}

	@RequestMapping("order_statistics")
	public String entryStatistics(Model model) {
		return "front/front_statistics";
	}

	@RequestMapping("get_order_statistics_data")
	@ResponseBody
	public Map<String,Object> getStatisticsData(String froms,String state){
		try {
			System.out.println("froms:"+froms+",state:"+state);
			if(froms==null || "".equals(froms) || state==null || "".equals(state)){
				return null;
			}
			long storeId = 6;
			String fromsArr[] = froms.split(",");
			String stateArr[] = state.split(",");
			List<Integer> fromsList = new ArrayList<>();
			List<Integer> stateList = new ArrayList<>();
			for(String fs:fromsArr){
				Integer fsInt = Integer.parseInt(fs);
				fromsList.add(fsInt);
			}

			for(String sl:stateArr){
				Integer slInt = Integer.parseInt(sl);
				stateList.add(slInt);
			}
			System.out.println(fromsList+","+stateList);
			Map<String, Object> zbMap = newOrderService.getTodayZbData(storeId,fromsList,stateList);
			List<Map<String, Object>> menuTopList = newOrderService.getTodayTopMenuData(storeId,fromsList,stateList);
			List<Map<String, Object>> orderList = newOrderService.getTodayOrderListData(storeId,fromsList,stateList);

			Map<String ,Object> map = new HashMap<>();
			map.put("zbMap",zbMap);
			map.put("menuTopList",menuTopList);
			map.put("orderList",orderList);
			return map;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
